<template>
  <div style="margin-top:15px;">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <!--<el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="所属机构" prop="partyGroupCode" style="margin-right:15px">
              <el-select v-model="form.partyGroupCode" placeholder="请选择机构" clearable style="width: 100%">
                <el-option
                  v-for="item in partyGroup"
                  :key="item.code"
                  :label="item.groupName"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>-->

      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="客户名称" prop="shipmentCode" style="margin-right:15px">
              <shipmentSelect ref="shipmentSelect" @setCustomerCode="setCustomerCode" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="计费类型" prop="chargingType" style="margin-right:15px">
              <costTypeSelect ref="costType" @queryCostType="selectCostType" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="单价" prop="price" style="margin-right:15px">
              <el-input v-model="form.price" placeholder="请输入单价" />
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="计费方式" prop="chargingMode" style="margin-right:15px">
              <el-select v-model="form.chargingMode" placeholder="请选择">
                <el-option value="" label="请选择" />
                <el-option value="1" label="吨" />
              </el-select>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    <!--  <el-row>
        <el-col :span="24">
          <div class="grid-content bg-purple">
            <el-form-item label="有效期" prop="validityDate" min-width="350"  style="margin-right:15px">
              <el-date-picker
                v-model="validityDate"
                value-format="yyyy-MM-dd"
                type="daterange"
                style="width:100%"
            range-separator="至"

                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
          </div>
        </el-col>
      </el-row>-->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="funcCancel">取 消</el-button>
      <el-button type="primary" @click="funcSubmit">确 定</el-button>
    </div>
  </div>
</template>
<script>
import Vue from 'vue'
import 'xe-utils'
import VXETable from 'vxe-table'
import 'vxe-table/lib/index.css'
import request from '@/utils/request'
import shipmentSelect from '@/views/components/shipmentSelect.vue'
import costTypeSelect from './costTypeSelect.vue'
Vue.use(VXETable)
export default {
  name: 'ChargingTypeEdit',
  components: {
    costTypeSelect,
    shipmentSelect
  },
  data() {
    return {
      emptyForm: {},
      selectCostTypeValue: [],
      // validityDate:'',
      form: {
        code: '',
        shipmentCode: '',
        shipmentName:'',
        partyGroupCode:'',
        chargingType: '',
        sysType: '',
        price: '',
        chargingMode: '',
      /*  validityDate:'',
        startDate: '',
        endDate: ''*/
      },
      shipmentData:[],
      partyGroup:[],
      rules: {
        partyGroupCode: [
          { required: true, message: '请选择所属机构', trigger: 'blur' }
        ],


        shipmentCode: [
          { required: true, message: '请选择客户', trigger: 'change' }
        ],
        chargingType: [
          { required: true, message: '请选择计费类型', trigger: 'blur' }
        ],
        sysType: [
          { required: true, message: '请输入系统类型', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请输入单价', trigger: 'blur' }

        ],
        chargingMode: [
          { required: true, message: '请选择计费方式', trigger: 'blur' }
        ]/*,
        validityDate: [
          { required: true, message: '请输入有效期', trigger: 'blur' ,
              validator: (rule, value, callback) =>{
                  if(this.validityDate==null||this.validityDate.length<2){
                      callback(new Error('请输入有效期'));
                  }else{
                      callback();
                  }
              }}
        ]*/
      }
    }
  },
  created() {
    // 保存空表单对象
    this.emptyForm = JSON.parse(JSON.stringify(this.form))
    this.findShipmentPartys()

  },
  methods: {
    // 获取客户
    findShipmentPartys() {
      request({
        url: '/arpa-basic-api/party/wmsShipment',
        method: 'get',
        params: { name: this.partyName }
      }).then(response => {
        this.shipmentData = response.data
      })
    },
    // 选择客户
    setCustomerCode(value) {
      this.form.shipmentCode = value;
      this.tableData = [];
      this.form.shipmentName = value.name
    },
    // 选择计费类型
    selectCostType(value) {
      this.form.chargingType = value
    },
    // 获取页面信息
    getInfo(code) {
      // 查询到数据之前先展示空表单
      this.form = JSON.parse(JSON.stringify(this.emptyForm))
      request({ url: `/arpa-basic-api/chargingType/${code}`, method: 'get' }).then(response => {
        this.form = response.data
        this.selectCostTypeValue = []
        this.selectCostTypeValue.push(this.form.sysType)
        this.selectCostTypeValue.push(this.form.chargingType)
        let str = this.formattershipment(this.form.shipmentCode)
        this.$refs.shipmentSelect.setSelectValue(str)
        this.$refs.costType.setSelectValue(this.selectCostTypeValue)
      /*  this.validityDate = [];
        this.validityDate[0] = this.form.startDate;
        this.validityDate[1] = this.form.endDate;
        this.form.validityDate = this.validityDate;*/
      })
    },
    // 翻译客户类型
    formattershipment(cellValue) {
      const sd = this.shipmentData.find(i => i.code === cellValue)
      return sd ? sd.name : ''
    },
    // 提交
    funcSubmit() {
      // this.form.validityDate = this.validityDate

      this.$refs['form'].validate((valid) => {
        if (valid) {
         /* this.form.startDate = this.form.validityDate[0]
          this.form.endDate = this.form.validityDate[1]*/
          const data = this.form
          request({ url: '/arpa-basic-api/chargingType', method: 'put', params: data }).then(response => {
            this.$refs.shipmentSelect.setSelectValue('')
            this.$message({ message: response.msg, type: 'success' })
            this.$emit('fatherEditMethod')
          })
        } else {
          return false
        }
      })
    },
    // 取消
    funcCancel() {
      this.$emit('fatherCancelMethod', 'edit')
    }

  }
}
</script>
<style lang="scss" scoped>
    .dialog-footer {
        margin:15px 0 15px 0;
        width: 175px;
        float: right;
    }
</style>
